package com.example.user.customviewdemo.layoutanimation;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.example.user.customviewdemo.R;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 使用ViewGroup的layoutAnimation属性为容器加载子view时使用动画
 */
public class LayoutAnimationActivity extends AppCompatActivity {

    @Bind(R.id.layout_animation_listview)
    ListView layoutAnimationListview;
    @Bind(R.id.add_button)
    Button addButton;

    private ArrayList<String> data = new ArrayList<>();
    private ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout_animation);
        ButterKnife.bind(this);
        addData();
        final ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, data);
        layoutAnimationListview.setAdapter(adapter);

        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addData();
                adapter.notifyDataSetChanged();
            }
        });

    }

    /**
     * 代码设置listview的LayoutAnimation
     */
    private void setLayoutAnimationByCode(){
        Animation anim = AnimationUtils.loadAnimation(this,R.anim.slid_left);
        LayoutAnimationController controller = new LayoutAnimationController(anim);
        controller.setDelay(1);
        controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
        controller.setAnimation(anim);

        layoutAnimationListview.setLayoutAnimation(controller);
        //layoutAnimationListview.startLayoutAnimation();
    }

    private void addData() {
        for (int i = 0; i < 5; i++) {
            data.add("王" + i);
        }
    }
}
